def GetTrea():
    # 定义红色、绿色、蓝色和黄色的HSV范围
    lower_red = np.array([0, 100, 100])
    upper_red = np.array([10, 255, 255])
    lower_green = np.array([45, 40, 40])
    upper_green = np.array([80, 220, 220])
    lower_blue = np.array([60, 80, 140])
    upper_blue = np.array([120, 140, 200])
    lower_yellow = np.array([16, 120, 120])
    upper_yellow = np.array([43, 255, 255])
    xr = xy = xb = xg = 0
    yr = yy = yb = yg = 0
    wr = wy = wb = wg = 0
    hr = hy = hb = hg = 0
    resultT = [0x41]
    # 初始化摄像头
    cap = cv2.VideoCapture(0)
    cnt = 0
    while True:
        # 从摄像头中获取图像
        ret, frame = cap.read()
        key = cv2.waitKey(1) & 0xFF
        cnt += 1
        if ret:
            # 转换图像为HSV格式
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            print(cnt)
            # 红色检测
            red_mask = cv2.inRange(hsv, lower_red, upper_red)
            red_contours, _ = cv2.findContours(red_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            red_contours = sorted(red_contours, key=lambda x: cv2.contourArea(x), reverse=True)
            if len(red_contours) > 0:
                (xr, yr, wr, hr) = cv2.boundingRect(red_contours[0])
                cv2.rectangle(frame, (xr, yr), (xr + wr, yr + hr), (0, 0, 255), 2)
                cv2.putText(frame, "Red", (xr, yr), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

            # 绿色检测
            green_mask = cv2.inRange(hsv, lower_green, upper_green)
            green_contours, _ = cv2.findContours(green_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            green_contours = sorted(green_contours, key=lambda x: cv2.contourArea(x), reverse=True)
            if len(green_contours) > 0:
                (xg, yg, wg, hg) = cv2.boundingRect(green_contours[0])
                cv2.rectangle(frame, (xg, yg), (xg + wg, yg + hg), (0, 255, 0), 2)
                cv2.putText(frame, "Green", (xg, yg), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

            # 蓝色检测
            blue_mask = cv2.inRange(hsv, lower_blue, upper_blue)
            blue_contours, _ = cv2.findContours(blue_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            blue_contours = sorted(blue_contours, key=lambda x: cv2.contourArea(x), reverse=True)
            if len(blue_contours) > 0:
                (xb, yb, wb, hb) = cv2.boundingRect(blue_contours[0])
                cv2.rectangle(frame, (xb, yb), (xb + wb, yb + hb), (255, 0, 0), 2)
                cv2.putText(frame, "Blue", (xb, yb), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)

            # 黄色检测
            yellow_mask = cv2.inRange(hsv, lower_yellow, upper_yellow)
            yellow_contours, _ = cv2.findContours(yellow_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            yellow_contours = sorted(yellow_contours, key=lambda x: cv2.contourArea(x), reverse=True)
            if len(yellow_contours) > 0:
                (xy, yy, wy, hy) = cv2.boundingRect(yellow_contours[0])
                cv2.rectangle(frame, (xy, yy), (xy + wy, yy + hy), (0, 255, 255), 2)
                cv2.putText(frame, "Yellow", (xy, yy), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2)

        if xr < xg and xr + wr > xg + wg and yr < yg and yr + hr > yg + hg:
            resultT.insert(0, 0x52)
            return resultT
            break
        elif xb < xy and xb + wb > xy + wy and yb < yy and yb + hb > yy + hy:
            resultT.insert(0, 0x42)
            return resultT
            break
        elif xr < xy and xr + wr > xy + wy and yr < yy and yr + hr > yy + hy:
            resultT.insert(0, 0x46)
            return resultT
            break
        elif xb < xg and xb + wb > xg + wg and yb < yg and yb + hb > yg + hg:
            resultT.insert(0, 0x46)
            return resultT
            break
        elif cnt == 200:
            resultT.insert(0, 0x46)
            return resultT
            break
        # 显示识别结果
        # cv2.imshow("Color Detection", frame)
        xr = xy = xb = xg = 0
        yr = yy = yb = yg = 0
        wr = wy = wb = wg = 0
        hr = hy = hb = hg = 0
    # 释放摄像头
    cap.release()

    # 关闭所有窗口
    cv2.destroyAllWindows()


import cv2
import numpy as np

if __name__ == "__main__":
    k = GetTrea()
